<template>
  <div class="login-page">
    <CpNavBar
      title="登录"
      right-text="注册"
      @click-right="gotoRegister"
    ></CpNavBar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="doLogin" ref="form">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="mobile"
        type="tel"
        :rules="mobileRules"
      ></van-field>
      <van-field
        v-if="isPass"
        placeholder="请输入密码"
        v-model="password"
        type="password"
        :rules="passwordRules"
      ></van-field>
      <van-field placeholder="短信验证码" v-else v-model="code">
        <template #button>
          <van-count-down
            v-if="isWatingCode"
            :time="60 * 1000"
            format="ss秒后重新发送"
            @finish="isWatingCode = false"
          ></van-count-down>
          <span v-else class="btn-send" @click="sendSMSCode">发送验证码</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useUserStore } from '@/stores';
import type { FormInstance } from 'vant';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
const router = useRouter();
const route = useRoute();
const gotoRegister = () => {
  router.push('/register');
};
const mobile = ref('13230000001');
const password = ref('abc12345');
const { setUser } = useUserStore();

//密码验证码切换
const isPass = ref(false);
//引入规则
import { mobileRules, passwordRules } from '@/utils/rules';
import { showToast } from 'vant';
// 登录接口
import { cpGetSMSCode, cpLoginByPassWord } from '@/api/user';
//执行登录逻辑
const doLogin = () => {
  //是否勾选
  if (!isAgree.value) {
    return showToast('请勾选协议');
  }
  cpLoginByPassWord({ password: password.value, mobile: mobile.value }).then(
    (res) => {
      // console.log(res.data.token);
      //存储+持久化
      // console.log(res.data);
      setUser(res.data);
      //跳转到之前的页面
      router.push((route.query.redirectURL as string) || '/user');
    }
  );
};
//
const isAgree = ref(false);
const form = ref<FormInstance | null>(null);
//发送验证码的方法
const code = ref('');
const sendSMSCode = () => {
  form.value
    ?.validate('mobile')
    .then(() => {
      //请求验证码
      isWatingCode.value = true;
      return cpGetSMSCode({ mobile: mobile.value, type: 'login' });
    })
    .then((res) => {
      // console.log(res);
      code.value = res.data.code;
    })
    .catch((err) => {
      console.log(err);
    });
};
//倒计时时间
// const time = ref(30 * 60 * 60 * 1000);

// 是否显示倒计时
const isWatingCode = ref(false);
</script>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
